import {message} from 'antd';

let ProjectService   = require('../../service/project');


export default {
  namespace : 'project',
  state:{
    loading:false,
    projectList:null
  },

  effects:{
    *queryProjectInfos(_, {call, put}){
      yield put({type:'showLoading'});
      console.log('Querying project infos.');
      const result  = yield call(ProjectService.queryProjectInfos);
      console.log(result);
      const {status, data}  = result;

      if(200 == status){
        const projectInfos  = data.items;
        yield put({type:'setProjectInfos', payload:projectInfos});
        yield put({type:'queryPageTrusty', payload:{pageSize:2, currentPage:1}});
      }
      else{
        message.error('获取跟单数据失败:' + data.toString());
      }

      yield put({type:'hideLoading'});
    },

    *queryTrustyLibraryInfos(_, {call, put}){
      console.log('Query trusty library infos');
      const result  = yield call(ProjectService.queryTrustyLibraryInfos);
      const {status, data}  = result;
      if(200 == status){
        console.log(data);
        yield put({type:'refresh'});
      }
      else{
        message.error('获取用户列表信息失败' + data.toString());
      }
    },

    *queryPageTrusty({payload:{pageSize, currentPage}}, {call, put, select}){
      console.log('Page size', pageSize);
      console.log('Current page', currentPage);


      const {projectList}  = yield select( state => state.project);
      console.log('projectList', projectList);
      if(projectList){
        // currentPage  begin at 1
        const bottomPos  = currentPage * pageSize ;
        const topPos  = bottomPos - pageSize + 1;
        console.log('Top  Bot', topPos, bottomPos);

        let isSomethingChanged  = false;
        let queryNewThing  = false;


        for(var pos = topPos; pos <= bottomPos && pos <= projectList.length; pos++){
          const result  = yield call(ProjectService.queryTrustyOfProject, projectList[pos-1].projectId);
          if(result != null  &&  result.fresh === true){
            console.log('ask to update trusty of project', projectList[pos-1].projectId);
            yield put({type:'updateProjectTrusty', payload:{projectId:projectList[pos-1].projectId, trustyIdList:result.data}});
          }
        }
      }
    }
  },


  reducers:{
    showLoading(state,_){
      return {
        loading:true,
        projectList:state.projectList
      };
    },
    hideLoading(state, _){
      return {
        loading:false,
        projectList:state.projectList
      }
    },
    setProjectInfos(state, {payload}){
      console.log('Set project infos');
      const newProjectList  = [...payload];
      const newState  = {
        loading:state.loading,
        projectList:newProjectList
      };

      return newState;
    },
    refresh(state, _){
      console.log('Refresh project table');
      return state;
    },

    updateProjectTrusty(state, {payload}){
      const {projectId, trustyIdList}  = payload;
      const newProjectList  = [...state.projectList];

      let refresh  = false;

      console.log('update project trusty reducer', newProjectList);
      if(newProjectList){
        for(let i = 0; i < newProjectList.length; i++){
          if( newProjectList[i].projectId === projectId){
          //  newProjectList[i].trustyIdList  = [...trustyIdList];
            console.log(projectId, 'update project trusty');

            return {
              loading:state.loading,
              projectList:newProjectList
            }
          }
        }
      }

      return state;
    }
  }
}
